package com.potenza.activiti.controller;

import com.potenza.activiti.security.SecurityUtil;
import org.activiti.api.process.model.ProcessInstance;
import org.activiti.api.process.model.builders.ProcessPayloadBuilder;
import org.activiti.api.process.model.payloads.StartProcessPayload;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.api.runtime.shared.query.Page;
import org.activiti.api.runtime.shared.query.Pageable;
import org.activiti.api.task.model.Task;
import org.activiti.api.task.model.builders.TaskPayloadBuilder;
import org.activiti.api.task.model.payloads.GetTasksPayload;
import org.activiti.api.task.runtime.TaskRuntime;
import org.activiti.engine.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("activiti")
public class ActivitiAddTaskController {

    @Autowired
    private ProcessRuntime processRuntime;//
    @Autowired
    private TaskRuntime taskRuntime;//

    @Autowired
    private SecurityUtil securityUtil;

    /**
     * 开启流程
     * @return
     */
    @PostMapping("/add/{businessKey}/{userId}")
    public Map submitBusiness(@PathVariable String businessKey,
                                 @PathVariable String userId){
        securityUtil.logInAs(userId);

        Map resultMap = new HashMap<String,Object>();
        String cfo_id = "cfo_0001_lisi";//查询CFO
        //开始流程
        Map vars = new HashMap<String,String>();//关联用户

        vars.put("user_id",userId);
        vars.put("cfo_id",cfo_id);//查询CFO
        String businessKey1 = businessKey;//当前用户创建的产品

        StartProcessPayload myProcess_1 = ProcessPayloadBuilder
                .start()
                .withProcessDefinitionKey("myProcess_cfoAdd")//指定的流程，配置文件里面
                .withVariables(vars)
                .withBusinessKey(businessKey)//关联业务
                .build();
        ProcessInstance pi = processRuntime.start(myProcess_1);
        System.out.println("流程实例ID：" + pi.getId());//流程实例ID：f
        resultMap.put("ProcessInstanceId",pi.getId());

        GetTasksPayload getTaskPayload = TaskPayloadBuilder
                .tasks()
                .withProcessInstanceId(pi.getId())//查询条件  流程ID
                .withAssignee(userId)//用户
                .build();
        Page<Task> taskPage = taskRuntime.tasks(
                Pageable.of(0, 10),getTaskPayload);

        if (taskPage.getTotalItems() > 0) {
            for (Task task : taskPage.getContent()) {
                System.out.println("任务：" + task);
                resultMap.put("taskId",task.getId());
                //taskRuntime.claim(TaskPayloadBuilder.claim().withTaskId(task.getId()).build());
                taskRuntime.complete(TaskPayloadBuilder.complete().withTaskId(task.getId()).build());
            }
        }

        return resultMap;
    }

    @Autowired
    TaskService taskService;

    @PostMapping("/add/cfo/{taskId}/{cfo_id}/{ceo_id}")
    public Task cfoDelegateTask(@PathVariable String taskId,
                                @PathVariable String cfo_id,
                                @PathVariable String ceo_id) {

        securityUtil.logInAs(cfo_id);
        Task task = taskRuntime.task(taskId);

        taskService.delegateTask(taskId,ceo_id);//加签  加了ceo

        return task;
    }

    @PostMapping("/add/ceo/{taskId}/{ceo_id}")
    public Task ceoResolveTask(@PathVariable String taskId,
                               @PathVariable String ceo_id) {

        securityUtil.logInAs(ceo_id);
        Task task = taskRuntime.task(taskId);

        taskService.resolveTask(taskId);//ceo 处理任务productaccess
        //taskService.complete(taskId);
        return task;
    }

    @PostMapping("/add/cfo/{taskId}/{cfo_id}")
    public Task cfoComplete(@PathVariable String taskId,
                            @PathVariable String cfo_id) {

        securityUtil.logInAs(cfo_id);
        Task task = taskRuntime.task(taskId);

        taskService.complete(taskId);//加签完后 自己再次完成任务
        return task;
    }

}
